﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weixin.crm
{
    public partial class login : System.Web.UI.Page
    {
        protected string openid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Session["openid"] + "") && !Request.Url.AbsoluteUri.StartsWith("http://localhost"))
                {
                    Response.Redirect("/getcode.aspx");
                }
                if (Request.Url.AbsoluteUri.StartsWith("http://localhost"))
                {
                    Session["openid"] = "od9r7wLndA4qoA4vBJgiYWu7qqrI5df3";
                }
                openid = Session["openid"] + "";
                string jsonstr = weixin.api.WXComm.GetHtmlResponse(basepage.Domain + "/api/webapi.ashx?rnd=" + DateTime.Now, "POST", string.Format("Action=wxlogin&openid={0}", openid));
                JObject json = JObject.Parse(jsonstr);
                if (bool.Parse(json.GetValue("success") + ""))
                {
                    HttpCookie cookies = new HttpCookie("token");
                    cookies.Path = "/";
                    cookies.HttpOnly = true;
                    cookies.Expires = DateTime.Now.AddMonths(1);
                    cookies.Value = json.GetValue("token") + "";
                    Response.SetCookie(cookies);

                    cookies = new HttpCookie("driveid");
                    cookies.Path = "/";
                    cookies.HttpOnly = true;
                    cookies.Expires = DateTime.Now.AddMonths(1);
                    cookies.Value = json.GetValue("driveid") + "";
                    Response.SetCookie(cookies);
                    Response.Redirect("main.aspx");
                }
            }
        }
    }
}